export default function ({ $axios, store, app, redirect, error, isServer }) {
  $axios.defaults.timeout = 20000
  $axios.defaults.maxContentLength = 10485760
  $axios.defaults.baseURL = (function () {
    let baseURL
    switch (process.env._ENV) {
      case 'development':
        // baseURL = 'http://192.168.1.142:8763'
        baseURL = 'https://wechat.junyanginfo.com'
        // baseURL = 'http://localhost:8080/nocache-proxy/'
        // baseURL = 'http://192.168.1.86:8763'
        // baseURL = 'http://192.168.1.169:8763'
        // baseURL = 'http://testapp.zhiyazhiyuan.com:8763/'
        // baseURL = 'http://39.104.2.117:8763'
        break
      case 'release':
        // baseURL = 'https://wechat.junyanginfo.com'
        baseURL = 'http://testapp.zhiyazhiyuan.com:8763'
        break
      case 'beta':
        // baseURL = 'https://wechat.junyanginfo.com'
        baseURL = 'http://testzhiyaapi.junyanginfo.com'
        break
      case 'prod':
        baseURL = ' https://wechat.junyanginfo.com'
        break
    }
    return baseURL
  })()
  /* 添加拦截器 */
  $axios.interceptors.request.use(function (config) {
    // console.log(config)
    if (config.url.indexOf('nocache-proxy') > -1) {
      config.baseURL = ''
    }
    const { token } = store.state.authInfo
    if (token) {
      config.headers['Authorization'] = `Bearer ${token}`
    }
    /* config.headers['Authorization'] = `Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJXRUIxNTU3NTg2NzU0NyIsImV4cCI6MTU0Mjg4MjY1MCwiaWF0IjoxNTQyMjc3ODUwfQ.7prI6J3UW5IageZ9HvKqOROMaIKOk4nNWV7ubV429lm5_mOy1ygroH72nu_G29DEJuDztAYUt-Tq8zeToo_WEA` */
    return config
  }, function (error) {
    return Promise.reject(error)
  })
  /* 添加响应拦截器 */
  $axios.interceptors.response.use(function (response) {
    return response.data
  }, function (err) {
    const errorObj = err.response ? err.response : { err: '网络请求错误' }
    console.log(errorObj.status)
    if (errorObj && errorObj.status === 401) {
      error({
        statusCode: 401,
        message: '您的登录状态已经过期，请重新登录。'
      })
    }
    return Promise.reject(errorObj)
  })
}
